<?php
class VersionController extends Zend_Controller_Action
{
	protected $_acl;
	protected $_userGroup;
	
	public function init()
	{
		$auth = Zend_Auth::getInstance();
		$userInfo = $auth->getStorage()->read();
		$groupBase = new GroupBase();
		if ($userInfo) {
			$userGroups = $groupBase->getUserGroup($userInfo->name);
		}
		$this->_userGroup = $userGroups[0]['name'] ? $userGroups[0]['name'] : "Users";
		$this->_acl = new Acl_PPMIS();
		$this->view->acl = $this->_acl;
		$this->view->userGroup = $this->_userGroup;
	}
	
	public function preDispatch()
	{
		$auth = Zend_Auth::getInstance();
		if($auth->getIdentity()) {
			$this->view->auth = "success";
			$userInfo = $auth->getStorage()->read();
			$this->view->userInfo = $userInfo;
		} else {
			$this->_redirect('auth/login');
		}
		$Configuration = new Configuration();
		$configurationData = $Configuration->getConfigurationInfo();
		$this->view->configuration = $configurationData;
		$this->view->current = "home";
	}
	
	public function openissuesAction()
	{
		$vid = $this->_getParam('vid');
		$projectId = $this->_getParam('projectId');
		$this->view->current = "project";
		$issue  = new Issue();
		$project = new Project();
		$projectVersion = new ProjectVersion();
		$component = new Component();
		
		$versionData = $projectVersion->getAdapter()->fetchRow($projectVersion->select()->where('id = ?',$vid));
		$versionData['pname'] = $project->getAdapter()->fetchOne($project->select()
														->from('project','pname')
														->where('id = ?',$projectId));
														
		$versionSummary = $issue->getVersionSummary($projectId,$vid);												
		$openIssues = $issue->getVersionOpenIssues($projectId, $vid);
		$totalIssues = 0;
		foreach ($versionSummary as $s) {
			$totalIssues = $totalIssues + $s['c'];
		}
		$componentsData = $component->getVersionData($projectId, $vid);
		$versionIssues = $issue->getVersionIssues($vid);
		
		$unscheduledIssues = $issue->getNoComponentIssues($projectId);
		
		$this->view->unscheduleIssues = $unscheduledIssues;
		$this->view->versionIssues = $versionIssues;
		$this->view->versionData = $versionData;
		$this->view->totalIssues = $totalIssues;
		$this->view->versionSummary = $versionSummary;
		$this->view->openIssues = $openIssues;
		$this->view->componentsData = $componentsData;
		$this->view->projectId = $projectId;
		
	}
	
	public function manageAction()
	{
		$projectVersion = new ProjectVersion();
		$projectId = $this->_getParam('project',1);
		$versionsData = $projectVersion->getProjectVersions($projectId);
		
		$form = new AddVersionForm();
		if ($this->getRequest()->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				//$releaseDate = $formData['releaseDate'];
				$versionData = array(
					'project' => $formData['project'],
					'vname' => $formData['vname'],
					'description' => $formData['description'],
					'released' => null,
					'url' => null,
					'releasedate' => date('Y-m-d h:i:s',$releaseDate),
				);
				$projectVersion->insert($versionData);
				$versionsData = $projectVersion->getProjectVersions($projectId);
				//Zend_Debug::dump($versionData);
			}
		}
		$versionsData = $projectVersion->getProjectVersions($projectId);
		$this->view->form = $form;
		$this->view->data = $versionsData;
		$this->view->auth = "success";
	}
	
	public function editAction()
	{
		$vid = $this->_getParam('vid');
		$form = new EditVersionForm();
		$projectVersion = new ProjectVersion();
		$pData = $projectVersion->getAdapter()->fetchRow($projectVersion->select()->where('id = ?', $vid));
		$form->populate($pData);
		if ($this->getRequest()->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				$id = $formData['id'];
				$projectVersion->update(array(
								'vname' => $formData['vname'],
								'description' => $formData['description'],
								'releasedate' => $formData['releasedate']
					), "id = $id");
			}
		}
		$this->view->versionName = $pData['vname'];
		$this->view->form = $form;
	}
	
}